<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <style>
        .el-table .warning-row {
            background: oldlace;
        }

        .el-table .success-row {
            background: #f0f9eb;
        }
    </style>


</head>
<body>
<div id="app">

    <el-form :inline="true" :model="UserData1" class="demo-form-inline">
        <el-form-item label="编号">
            <el-input v-model="UserData1.id" placeholder="编号"></el-input>
        </el-form-item>
        <el-form-item label="姓名">
            <el-input v-model="UserData1.username" placeholder="姓名">
            </el-input>
        </el-form-item>
        <el-form-item label="密码">
            <el-input v-model="UserData1.password" placeholder="密码"></el-input>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="onSubmit(scope.row)">查询</el-button>
        </el-form-item>

    </el-form>


    <el-row>

        <el-button type="success" plain @click="dialogFormVisible = true">添加数据</el-button>

        <el-dialog title="个人信息" :visible.sync="dialogFormVisible">
            <el-form :model="UserData2">
                <el-form-item label="姓名" :label-width="formLabelWidth">
                    <el-input v-model="UserData2.username" autocomplete="off" placeholder="请输入姓名"></el-input>
                </el-form-item>
                <el-form-item label="密码" :label-width="formLabelWidth">
                    <el-input v-model="UserData2.password" placeholder="请输入密码">
                    </el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="insert()">确 定</el-button>
            </div>
        </el-dialog>


        <el-button type="danger" plain>批量删除</el-button>
    </el-row>

    <!--添加数据对话框表单 更改数据-->
    <el-dialog
            title="修改个人数据"
            :visible.sync="updateVisible"
            width="30%">
        <el-form ref="form" :model="UserData1" label-width="80px">
            <el-form-item label="姓名">
                <el-input v-model="UserData1.username"></el-input>
            </el-form-item>

            <el-form-item label="密码">
                <el-input v-model="UserData1.password"></el-input>
            </el-form-item>

            <el-form-item>
                <el-button type="primary" @click="updateUser">提交</el-button>
                <el-button @click="updatedelete">取消</el-button>
            </el-form-item>

        </el-form>
    </el-dialog>


    <el-table
            ref="multipleTable"
            :data="UserData"
            tooltip-effect="dark"
            style="width: 100%"
            :row-class-name="tableRowClassName">
        @selection-change="handleSelectionChange">
        <el-table-column
                type="selection"
                width="55">
        </el-table-column>

        <el-table-column
                prop="id"
                label="编号"
                width="180"
                align="center">

        </el-table-column>
        <el-table-column
                prop="username"
                label="姓名"
                width="180"
                align="center">
        </el-table-column>

        <el-table-column
                prop="password"
                label="密码"
                align="center">
        </el-table-column>
        <el-table-column
                prop="operate"
                label="操作"
                align="center">
            <template slot-scope="scope">
                <el-button type="primary" icon="el-icon-edit" circle @click="showEditView(scope.row)"></el-button>
                <el-button type="danger" icon="el-icon-delete" circle @click="deleteHandler(scope.row)"></el-button>
            </template>
        </el-table-column>

    </el-table>


    <!--&lt;!&ndash; <el-pagination
             background
             layout="prev, pager, next"
             :total="1000">
     </el-pagination>
     -->


</div>


<script src="js/vue.js"></script>
<script src="element-ui/lib/index.js"></script>
<link rel="stylesheet" href="element-ui/lib/theme-chalk/index.css">
<script src="js/axios-0.18.0.js"></script>
<script>

    new Vue({
        el: "#app",
        mounted() {
            var _this = this;
            axios({
                method: "get",
                url: "http://localhost:8080/MybatisDemo2_war_exploded/SelectAll"
            }).then(function (resp) {
                _this.UserData = resp.data;
            })
        },
        data() {
            return {
                //表格数据
                UserData: [],
                dialogTableVisible: false,
                dialogFormVisible: false,
                updateVisible: false,
                formLabelWidth: '120px',
                //定义表单数据模型
                UserData1: {
                    id: "",
                    username: "",
                    password: ""
                },
                UserData2: {
                    id: "",
                    username: "",
                    password: ""
                }
            }
        },
        methods: {
            Init() {
                var _this = this;
                axios({
                    method: "get",
                    url: "http://localhost:8080/MybatisDemo2_war_exploded/SelectAll"
                }).then(function (resp) {
                    _this.UserData = resp.data;
                })
            },
            tableRowClassName({row, rowIndex}) {
                if (rowIndex === 1) {
                    return 'warning-row';
                } else if (rowIndex === 3) {
                    return 'success-row';
                }
                return '';
            },
            updatedelete() {
                this.updateVisible = false;
                var _this = this;
                axios({
                    method: "get",
                    url: "http://localhost:8080/MybatisDemo2_war_exploded/SelectAll"
                }).then(function (resp) {
                    _this.UserData = resp.data;
                })

            },
            handleSelectionChange(val) {
                this.multipleSelection = val;

            },
            insert() {
                axios({
                    method: "post",
                    url: "http://localhost:8080/MybatisDemo2_war_exploded/Insert",
                    data: this.UserData2
                }).then(function (response) {
                    var _this = this;
                    if (response.data) {
                        _this.dialogFormVisible = false;
                        location.href = "http://localhost:8080/MybatisDemo2_war_exploded/backend.html"
                    }
                })
            },
            //删除数据
            deleteHandler(row) {
                this.UserData2.id = row.id;

                this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    //用户点击确认按钮
                    axios({
                        method: "post",
                        url: "http://localhost:8080/MybatisDemo2_war_exploded/DeleteOne",
                        data: this.UserData2
                    }).then(resp => {
                        var _this = this;
                        if (resp.data) {
                            location.href = "http://localhost:8080/MybatisDemo2_war_exploded/backend.html";
                            this.$message({
                                type: 'success',
                                message: '删除成功!'
                            });
                        }
                    })

                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },

            //修改数据
            showEditView(row) {
                var _this = this;
                _this.UserData1 = row;
                _this.updateVisible = true;
            },
            updateUser() {
                var _this = this;
                //发送ajax请求
                axios({
                    method: "post",
                    url: "http://localhost:8080/MybatisDemo2_war_exploded/Update",
                    data: _this.UserData1
                }).then(function (resp) {
                    if (resp.data) {
                        _this.updateVisible = false;
                        location.href = "http://localhost:8080/MybatisDemo2_war_exploded/backend.html";
                    }
                })
            },
            onSubmit() {
                var _this = this;
                axios({
                    method: "post",
                    url: "http://localhost:8080/MybatisDemo2_war_exploded/SelectOne",
                    data: _this.UserData1
                }).then(function (resp) {
                    _this.UserData.id = _this.UserData1.id
                })

            }
        }

    })

</script>
</body>
</html>